﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using iBook2.Models;

namespace iBook2.Controllers
{
    public class TransactionController : ApiController
    {
        private iBookEntities db = new iBookEntities();

        // GET api/Transaction
        public IEnumerable<GiaoDich> GetGiaoDiches()
        {
            var giaodiches = db.GiaoDiches.Include(g => g.HoaDon).Include(g => g.Sach);
            return giaodiches.AsEnumerable();
        }

        // GET api/Transaction/5
        public GiaoDich GetGiaoDich(string id)
        {
            GiaoDich giaodich = db.GiaoDiches.Find(id);
            if (giaodich == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            return giaodich;
        }

        // PUT api/Transaction/5
        public HttpResponseMessage PutGiaoDich(string id, GiaoDich giaodich)
        {
            if (ModelState.IsValid && id == giaodich.MaGiaoDich)
            {
                db.Entry(giaodich).State = EntityState.Modified;

                try
                {
                    db.SaveChanges();
                }
                catch (DbUpdateConcurrencyException)
                {
                    return Request.CreateResponse(HttpStatusCode.NotFound);
                }

                return Request.CreateResponse(HttpStatusCode.OK);
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        // POST api/Transaction
        public HttpResponseMessage PostGiaoDich(GiaoDich giaodich)
        {
            if (ModelState.IsValid)
            {
                db.GiaoDiches.Add(giaodich);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, giaodich);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = giaodich.MaGiaoDich }));
                return response;
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        // DELETE api/Transaction/5
        public HttpResponseMessage DeleteGiaoDich(string id)
        {
            GiaoDich giaodich = db.GiaoDiches.Find(id);
            if (giaodich == null)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            db.GiaoDiches.Remove(giaodich);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            return Request.CreateResponse(HttpStatusCode.OK, giaodich);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}